﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Text.RegularExpressions;

namespace University.Workshop.Business
{
    public static class UserManager
    {
        /*private bool ValidData()
        {
        }*/


        public static User RegisterNewUser(
            string id,
            string firstName,
            string lastName,
            string telephone,
            string cellPhone,
            string address,
            string type,
            string logOn,
            string password,
            string retypePassword)
        {

            if (string.IsNullOrEmpty(id) ||
                string.IsNullOrEmpty(firstName) ||
                string.IsNullOrEmpty(lastName) ||
                string.IsNullOrEmpty(telephone) ||
                string.IsNullOrEmpty(cellPhone) ||
                string.IsNullOrEmpty(address) ||
                string.IsNullOrEmpty(type) ||
                string.IsNullOrEmpty(logOn) ||
                string.IsNullOrEmpty(password) ||
                string.IsNullOrEmpty(retypePassword))
            {
                throw new DataValidationException("Todos los campos son requeridos para el registro.");
            }

            //string namePattern = "^[A-Za-z]? ?A-Za-z- ?A-Za-z]";
            //string namePattern = "^([a-zA-Z\\s'-]+)";
            //string namePattern = "^[a-zA-Z'][a-zA-Z'- ]*[a-zA-Z']?$";
            //string namePattern = "^[A-Z][a-zA-Z]*$";
            //string namePattern = "^[A-Za-z'- ]*[A-Za-z]?$"; 

            string namePattern = "^([a-zA-Z-']+|[a-zA-Z-']+\\s[a-zA-Z]+)$";
            if (!(Regex.IsMatch(firstName, namePattern)))
            {
                throw new DataValidationException("Nombre inválido, no puede contener caráceteres no permitidos (por ej: números).");
            }

            if (!(Regex.IsMatch(lastName, namePattern)))
            {
                throw new DataValidationException("Apellido inválido, no puede contener caráceteres no permitidos (por ej: números).");
            }

            string idPattern = "\\d{5,20}";
            if (!(Regex.IsMatch(id, idPattern)))
            {
                throw new DataValidationException("Carnet inválido, debe tener solo números, mínimo 5.");
            }

            string telephonePattern = "\\d{7}";
            if (!(Regex.IsMatch(telephone, telephonePattern)))
            {
                throw new DataValidationException("Teléfono inválido, debe tener 7 números.");
            }

            string cellPhonePattern = "\\d{8}";
            if (!(Regex.IsMatch(cellPhone, cellPhonePattern)))
            {
                throw new DataValidationException("Celular inválido, debe tener 8 números.");
            }

            /*string addressPattern = "^([a-zA-Z.0-9]+|[a-zA-Z]+\\s[a-zA-Z0-9]+)$";
            if (!(Regex.IsMatch(address, addressPattern)))
            {
                throw new DataValidationException("Dirección inválida.");
            }*/

            //UserDataAccess data = new UserDataAccess();

            User existingUser = UserDataAccess.GetUserByLogOn(logOn);
            if (existingUser != null)
            {
                throw new DataValidationException("El nombre de usuario no está disponible, ya se encuentra registrado.");
            }

            existingUser = UserDataAccess.GetUserById(id);
            if (existingUser != null)
            {
                throw new DataValidationException("El carnet de identidad ya se encuentra registrado en el sistema.");
            }

            string passwordPattern = "\\w{8,20}";
            if (!(Regex.IsMatch(password, passwordPattern)))
            {
                throw new DataValidationException("Password inválido, debe tener de 8 a 20 carácteres.");
            }

            if (password != retypePassword)
            {
                throw new DataValidationException("Los passwords introducidos no coinciden. Vuelva a intentarlo.");
            }

            User newUser = new User {
                UserId = id, 
                FirstName = firstName, 
                LastName = lastName, 
                LandPhone = telephone, 
                CellPhone = cellPhone, 
                Address = address, 
                Role = type, 
                LogOn = logOn, 
                Password = password };
            UserDataAccess.InsertUser(newUser);
            return newUser;
        }

        public static User GetUserByUserName(string userName)
        {
            return UserDataAccess.GetUserByUserName(userName);
        }

        public static User ShowUser(string idUser)
        {
            User user = UserDataAccess.GetUserById(idUser);
            return user;
        }
    }
}
